package com.tips {
	import com.bases.Margin;
	import com.bases.TUIComponent;
	import com.skin.SkinManage;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import org.bytearray.display.ScaleBitmap;

	/**
	 * 鼠标提示面板类
	 * @author tamt
	 */
	public class TipPanel extends TUIComponent {

		public static const TIPPANEL : Boolean = false;
		public static const SHOTERTIPPANEL : Boolean = true;
		public static var MAX_WIDTH : uint = 200;

		//////////////////////////////////////
		//////////private variables///////////
		//////////////////////////////////////
		internal var target : DisplayObject;
		private var _docked : Boolean;		//面板是否处于悬停状态
		private var offsetX : uint = 16;
		private var offsetY : uint = 20;
		private var _bg : ScaleBitmap;		//背景图
		private var _margin : Margin;
		//存放内容的临时容器, 用于拍照.
		private var tmpContainer : Sprite;
		//拍照的位图.
		private var _bmp : Bitmap;
		//开启位图缓存
		private var _cacheAsBitmap : Boolean;
		//悬停时间(毫秒), 0即为不侦听悬停.
		private var _dockDelay : uint = 0;
		//自动调整坐标，使之始终显示在舞台内。
		
		//////////////////////////////////////
		//////////public variables////////////
		//////////////////////////////////////
		public var keepInStage : Boolean = true;

		//////////////////////////////////////
		////////////setter, getter////////////
		//////////////////////////////////////
		/**
		 * 鼠标停留多长时间后, tip面板钉住.
		 */
		public function set dockDelay(delay : uint) : void {
			this._dockDelay = delay;
		}

		/**
		 * 鼠标停留多长时间后, tip面板钉住.
		 */
		public function get dockDelay() : uint {
			return this._dockDelay;
		}

		public function get docked() : Boolean {
			return _docked;
		}

		public function TipPanel() {
			
			this.mouseChildren = false;
			_margin = new Margin();
			//
			tmpContainer = new Sprite();
			super.addChild(tmpContainer);
			//			
			_bg = new ScaleBitmap(SkinManage.getInstance().getBitmapData('TitleBGBmp'));
			_bg.scale9Grid = new Rectangle(7, 7, 16, 9);
			addChild(_bg);
			_bg.filters = [new DropShadowFilter()];

			this.addEventListener(MouseEvent.ROLL_OUT, onRollOut, false, 0, true);			this.addEventListener(MouseEvent.ROLL_OVER, onRollOver, false, 0, true);
			
			//
			this.addEventListener(Event.RESIZE, onResize);
		}

		private var _isMouseOn : Boolean;

		public function get isMouseOn() : Boolean {
			return _isMouseOn;
		}

		private function onRollOut(evt : MouseEvent) : void {
			_isMouseOn = false;
		}

		private function onRollOver(evt : MouseEvent) : void {
			_isMouseOn = true;
		}

		/**
		 * 开始悬停时
		 */
		public function onDock() : void {
			if(!this.docked) {
				this.cacheAsBitmap = false;
				
				this._docked = true;
				this.mouseEnabled = true;
				this.mouseChildren = true;
				this.resize();
				
				//调用子的onDock函数
				var l : int = 1;
				var child : DisplayObject;
				while(l < numChildren) {
					child = this.getChildAt(l++);
					if(child is TipPanelContent) {
						(child as TipPanelContent).onDock();
					}
				}
				
			}
		}

		/**
		 * 结束悬停.
		 */
		public function onUnDock() : void {
			if(this.docked) {
				this._docked = false;
				this.resize();
				this.mouseEnabled = false;
				this.mouseChildren = false;
				
				//调用子的onUnDock函数
				var l : int = 1;
				var child : DisplayObject;
				while(l < numChildren) {
					child = this.getChildAt(l++);
					if(child is TipPanelContent) {
						(child as TipPanelContent).onUnDock();
					}
				}
				
			}
		}

		/**
		 * 重新计算长宽.
		 */
		public function resize() : void {
			tmpContainer.removeChild(_bg);
			graphics.clear();

			var w : Number = tmpContainer.width;
			var h : Number = tmpContainer.height;
			_bg.setSize(w + _margin.left + _margin.right, h + _margin.bottom + _margin.top);
			_bg.x = -_margin.left;
			_bg.y = -_margin.top;
			
			tmpContainer.addChildAt(_bg, 0);
			
			if(this._docked) {
				graphics.beginFill(0xffff00, 0);
				graphics.drawRect(this.stage.mouseX < this.x ? (-(offsetX + 2)) : 0, this.stage.mouseY < this.y ? -(offsetY + 2) : -_margin.top, this.width + offsetX - _margin.right, this.height + offsetY - _margin.bottom);
				graphics.endFill();
				
			}
			
//			if(this._cacheAsBitmap) {
//				this._bmp.bitmapData.dispose();
//				this._bmp.bitmapData = SnapShoter.snap(tmpContainer);
//			}
		}

		private function onResize(evt : Event) : void {
			resize();
		}

		/**
		 * 校正tip的位置，保证它在舞台内显示
		 */
		private function reviseTipPanelPos() : void {
			if(!this.stage)return;
			
			var gpt : Point = this.parent.localToGlobal(new Point(x, y));
			
			var needRevise : Boolean;
			if(gpt.x > stage.stageWidth - width) {
				gpt.x = gpt.x - this.width - 16;
				needRevise = true;
			}
			
			if(gpt.x < 0) {
				gpt.x = 0;
				needRevise = true;
			}
			if(gpt.y > stage.stageHeight - height) {
				gpt.y = gpt.y - height - 20;
				needRevise = true;
			}
			if(gpt.y < 0) {
				gpt.y = 0;
				needRevise = true;
			}
			
			if(needRevise) {
				gpt = this.parent.globalToLocal(gpt);
				this.x = gpt.x;
				this.y = gpt.y;
			}
		}

		//////////////////////////////////////
		/////////public functions/////////////
		//////////////////////////////////////
		override public function setPos(x : Number, y : Number) : void {
			this.x = x + offsetX;
			this.y = y + offsetY;
			
			if(keepInStage)reviseTipPanelPos();
		}

		/**
		 * 清理内容.
		 */
		public function clear() : void {
			while(tmpContainer.numChildren > 1) {
				tmpContainer.removeChildAt(1);
			}
			this._bg.visible=true;		//addChildAt(_bg,0);
			this.dockDelay = 0;
		}
		
		/**
		 * 清除所有的内容 
		 * 
		 */		
		public function allClear():void{
			this.clear();
			this._bg.visible=false;
			//this.removeChildAt(0);
		}

		override public function addChild(child : DisplayObject) : DisplayObject {
			var t : DisplayObject = tmpContainer.addChild(child);
			resize();
			return t;
		}

		override public function getChildByName(childName : String) : DisplayObject {
			return tmpContainer.getChildByName(childName);
		}
	}
}
